package com.dunshan.dunshanmongodb.controller;


import com.alibaba.fastjson.JSON;
import com.dunshan.dunshanmongodb.pojo.User;
import com.dunshan.dunshanmongodb.service.MongoTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class IndexPage {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    MongoTestService service;

    @GetMapping("/index")
    @ResponseBody
    public Object index() {
        HashMap<String, String> map = new HashMap<>();
        map.put("ss", "dd");

        return map;
    }

    @GetMapping("/moreTwo/{dbName}")
    @ResponseBody
    public Object moreTwo(@PathVariable("dbName") String dbName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ok", "moreTwo");

//        List<User> users = mongoTemplate.findAll(User.class);
        List<User> users = service.findAll();
        map.put("data", JSON.toJSON(users));
        return map;
    }


    @GetMapping("/moreDb/{dbName}")
    @ResponseBody
    public Object moreDb(@PathVariable("dbName") String dbName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ok", "moreDb");
        List<User> users = mongoTemplate.findAll(User.class);
        map.put("data", JSON.toJSON(users));
        return map;
    }


}
